const {resolve} = require('path')
const {spawn} = require('child_process')
const rollup = require('rollup')
const electron = require('electron')
const {waitOn} = require('../util/waitOn.js')
const {terser} = require('rollup-plugin-terser')
//监控渲染进程是否启动

// 渲染进程启动成功后
let argv = process.argv.slice(2)

if (argv[0] === '--watch') {  //运行监听
    waitOn(1314, 1000).then(() => {
        let child = null
        const watcher = rollup.watch([
            {
                input: 'src/main/index.js',
                output: {
                    file: 'app/dist/main/index.js',
                    format: 'cjs'
                },
                watch: {
                    include: 'src/main/index.js',//监控主进程代码改变
                    exclude: 'node_modules/**'
                }
            },
            {
                input: 'src/preload/preload.js',
                output: {
                    file: 'app/dist/preload/preload.js',
                    format: 'cjs'
                },
                watch: {
                    include: 'src/preload/preload.js',//监控主进程代码改变
                    exclude: 'node_modules/**'
                }
            }
        ])
        // 开启打包监听，主进程被改变后重新打包
        watcher.on('event', e => {
            if (e.code === 'END') {
                // console.log('主进程代码被修改，重新打包完成 ')
                //暂时好像，，没什么用，好似无法成功杀死子进程
                if (child) {
                    // console.log('进程已存在')
                    child.kill()
                }
                child = spawn(electron, [resolve(__dirname, '../app/dist/main/index.js')], {
                    detached: false //禁止子进程独立出父进程
                });
                child.stdout.on('data', function (data) {
                    //stdout返回的log数据会带有\n，需要处理
                    let arr = data.toString().split('\n')
                    for (let i = 0; i < arr.length; i++) {
                        if (i < (arr.length - 1)) {
                            console.log(arr[i])
                        } else {
                            if (arr[i] !== '') {
                                console.log(arr[i])
                            }
                        }
                    }
                })
            }
        })

    })
}
else if (argv[0] === '--build') { //打包
    (async function () {
        const mainBundle = await rollup.rollup({
            input: 'src/main/index.js',
            plugins: [terser()],
        });
        const preloadBundle = await rollup.rollup({
            input: 'src/preload/preload.js',
            plugins: [terser()],
        });
        console.log(mainBundle.imports); // an array of external dependencies
        console.log(mainBundle.exports); // an array of names exported by the entry point
        console.log(mainBundle.modules); // an array of module objects
        await mainBundle.write({
            file: 'app/dist/main/index.js',
            format: 'cjs'
        });
        await preloadBundle.write({
            file: 'app/dist/preload/preload.js',
            format: 'cjs'
        });
    }())

}
